<?php
class Group extends AppModel{
    public $name = 'Group';
    public $actsAs = array('Acl'=>'requester');
    public $validate = array('name' =>array('notempty'));
    public $hasMany = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'group_id',
            'dependent' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    /* After save callback which is called when perform add and edit actions*/
    public function afterSave($created){
        if(!$created){
            // its an edit, we have to update the tree
            $data = $this->read();
            $parent_id = $data['Group']['parent_id'];
            $aro = new Aro();
            $aro_record = $aro->findByForeignKey($this->id);
            $parent_record = $aro->findByForeignKey($parent_id);
            if(empty($aro_record)){
                // orphaned child
                $this->Aro->save(array(
                    'model' => $this->name,
                    'foreign_key' => $this->id,
                    'alias' => $this->name . ':' . $this->id,
                    'parent_id' => $parent_record['Aro']['id']
                ));
            }else{
                // just moving nodes
                $this->Aro->save(array(
                    'model' => $this->name,
                    'foreign_key' => $this->id,
                    'alias' => $this->name.':'.$this->id,
                    'parent_id' => $parent_record['Aro']['id'],
                    'id' => $aro_record['Aro']['id']
                ));
            }
        }
        return true;
    }
    public function parentNode(){
        // This should be the alias of the parent $model::$id
        $data = $this->read();
        // This needs to be unique
        return 'Group:'.$data['Group']['parent_id'];
    }
}
?>